package com.carrental.api.ums.service.impl;

import cn.dev33.satoken.config.SaTokenConfig;
import cn.dev33.satoken.secure.BCrypt;
import cn.hutool.core.util.DesensitizedUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carrental.common.exception.OperationException;
import com.carrental.api.common.query.CommonQueryParam;
import com.carrental.api.common.utils.CommonQueryWrapper;
import com.carrental.api.ums.domain.UmsUser;
import com.carrental.api.ums.domain.bo.UmsUserBo;
import com.carrental.api.ums.domain.vo.UmsUserVo;
import com.carrental.api.ums.mapper.UmsUserMapper;
import com.carrental.api.ums.service.IUmsUserService;
import com.carrental.common.utils.StpUmsUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 会员信息Service业务层处理
 *
 * @author liangyi
 * @date 2022-03-20
 */
@Slf4j
@Service
public class UmsUserServiceImpl extends ServiceImpl<UmsUserMapper, UmsUser> implements IUmsUserService {
    @Autowired
    private SaTokenConfig saTokenConfig;

    /**
     * 增加用户
     *
     * @param userBo bo
     * @return is
     */
    @Override
    public Boolean create(UmsUserBo userBo) {
        UmsUser user = this.getOne(
            new QueryWrapper<UmsUser>().lambda()
                .eq(UmsUser::getTelephone, userBo.getTelephone())
                .eq(UmsUser::getIsDeleted, false)
        );
        if (user != null) {
            throw new OperationException("手机号已存在，请检查");
        }
        user = new UmsUser();
        BeanUtils.copyProperties(userBo, user);
        //密码加密
        user.setPassword(BCrypt.hashpw(user.getPassword()));
        if (!this.save(user)) {
            throw new OperationException("新增用户失败！");
        }
        return true;
    }

    /**
     * 删除用户
     *
     * @param id id
     * @return is
     */
    @Override
    public Boolean delete(Long id) {
        UmsUser user = new UmsUser();
        user.setIsDeleted(true);
        if (!this.updateById(user)) {
            throw new OperationException("删除失败！");
        }
        return true;
    }

    /**
     * 增加用户
     *
     * @param userBo bo
     * @return is
     */
    @Override
    public Boolean update(Long id, UmsUserBo userBo) {
        UmsUser user = new UmsUser();
        BeanUtils.copyProperties(userBo, user, "user_id");
        if (!this.updateById(user)) {
            throw new OperationException("删除失败！");
        }
        return true;
    }

    /**
     * 增加用户
     *
     * @param id bo
     * @return is
     */
    @Override
    public UmsUserVo detail(Long id) {
        return this.baseMapper.detail(
            new QueryWrapper<UmsUser>()
                .lambda()
                .eq(UmsUser::getUserId, id)
        );
    }

    /**
     * 根绝手机号查找用户
     *
     * @param telephone@return is
     */
    @Override
    public UmsUser getUserByTelephone(String telephone) {
        return this.getOne(
            new QueryWrapper<UmsUser>()
                .lambda()
                .eq(UmsUser::getTelephone, telephone)
        );
    }

    /**
     * 获取当前登录用户信息
     *
     * @return UmsUserVo
     */
    @Override
    public UmsUserVo getMyInfo() {
        log.info("satoken配置信息：" + saTokenConfig.toString());
        Long currentId = StpUmsUtil.getLoginIdAsLong();
        return detail(currentId);
    }

    /**
     * 用户分页
     *
     * @param commonQueryParam 查询参数
     * @return is
     */
    @Override
    public IPage<UmsUserVo> userPage(CommonQueryParam commonQueryParam) {
        IPage<UmsUserVo> userPage= this.baseMapper.pageList(
            new Page<>(commonQueryParam.getCurrent(), commonQueryParam.getSize()),
            new CommonQueryWrapper<UmsUser>(commonQueryParam)
                .lambda()
                .eq(UmsUser::getIsDeleted, false)
        );
        desensitized(userPage.getRecords());
        return userPage;
    }

    /**
     * 用户列表
     *
     * @param commonQueryParam 查询参数
     * @return list
     */
    @Override
    public List<UmsUserVo> userList(CommonQueryParam commonQueryParam) {
        List<UmsUserVo> userList= this.baseMapper.pageList(
            new CommonQueryWrapper<UmsUser>(commonQueryParam)
                .lambda()
                .eq(UmsUser::getIsDeleted, false)
        );
        desensitized(userList);
        return userList;
    }

    /**
     * 对某些信息脱敏
     */
    public void desensitized(List<UmsUserVo> userList){
        userList.forEach(user->{
            user.setIdCardNumber(DesensitizedUtil.idCardNum(user.getIdCardNumber(),4,6));
        });
    }
}
